//
//  XGDragBaseViewController.m
//  YiCuiMiaoZhang
//
//  Created by Sings on 2018/4/11.
//  Copyright © 2018 益萃网络科技（中国）有限公司. All rights reserved.
//

#import "DragBaseViewController.h"

@interface DragBaseViewController () <UITableViewDelegate>

@property (assign, nonatomic) CGFloat orignY;

@property (strong, nonatomic) UITableViewCell *cell;

@property (strong, nonatomic) UIImageView *cellImage;

@property (nonatomic, assign) CGFloat cellHight;

@property (nonatomic, assign) CGFloat cellWidth;

@property (nonatomic, strong) NSMutableDictionary *indexPathDic;

@end

@implementation DragBaseViewController

- (NSMutableDictionary *)indexPathDic {
    if (_indexPathDic == nil) { _indexPathDic = [NSMutableDictionary dictionary]; }
    return _indexPathDic;
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)cellViewAddPan:(UIView *)cellView {
    UILongPressGestureRecognizer *pan =
        [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(panReg:)];
    [cellView addGestureRecognizer:pan];
}

- (void)panReg:(UILongPressGestureRecognizer *)recognise {

    CGPoint currentPoint = [recognise locationInView:self.tableView];
    currentPoint         = [self.tableView convertPoint:currentPoint toView:self.view];
    CGPoint tableVPoint  = [recognise locationInView:self.tableView];

    if (recognise.state == UIGestureRecognizerStateBegan) {
        self.tableView.userInteractionEnabled = NO;
        _cellHight                            = 80;
        _cellWidth                            = ([UIScreen mainScreen].bounds.size.height);
        _orignY                               = currentPoint.y;
        _path                                 = [_tableView indexPathForRowAtPoint:tableVPoint];
        _oldPath                              = [_tableView indexPathForRowAtPoint:tableVPoint];
        _cell                                 = [_tableView cellForRowAtIndexPath:_path];
        _cellImage                            = [UIImageView new];
        _cellImage.image                      = [self getImageWithCell:_cell];
        _cell.hidden                          = YES;
        _cellImage.frame =
            CGRectMake(0, currentPoint.y - (_cellHight * 0.5), _cellWidth, _cellHight);
        _cellImage.backgroundColor = [UIColor clearColor];
        [self.view addSubview:_cellImage];
    } else if (recognise.state == UIGestureRecognizerStateChanged) {
        _cellImage.frame =
            CGRectMake(0, currentPoint.y - (_cellHight * 0.5), _cellWidth, _cellHight);
        if (currentPoint.y > _orignY) {
            if (currentPoint.y - _orignY > (_cellHight * 0.5)) {
                NSIndexPath *newIndexPath =
                    [NSIndexPath indexPathForRow:_path.row + 1 inSection:_path.section];
                if (newIndexPath.row < self.rowNum) {
                    [self.tableView moveRowAtIndexPath:_path toIndexPath:newIndexPath];
                    _path = newIndexPath;
                    _orignY += _cellHight;
                }
            }
        } else {
            if (_orignY - currentPoint.y > (_cellHight * 0.5)) {
                NSIndexPath *newIndexPath =
                    [NSIndexPath indexPathForRow:_path.row - 1 inSection:_path.section];
                if (newIndexPath.row >= 0) {
                    [self.tableView moveRowAtIndexPath:_path toIndexPath:newIndexPath];
                    _path = newIndexPath;
                    _orignY -= _cellHight;
                }
            }
        }
    } else if (recognise.state == UIGestureRecognizerStateEnded) {
        self.tableView.userInteractionEnabled = YES;
        [_cellImage removeFromSuperview];
        _cellImage   = nil;
        _cell.hidden = NO;
        [self cellDataOldIndex];
    }
}

- (void)cellDataOldIndex {
}

- (UIImage *)getImageWithCell:(UITableViewCell *)cell {
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(_cellWidth, _cellHight), NO, 1.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, CGRectMake(0, 3, _cellWidth, _cellHight),
                       [UIImage imageNamed:@"dragimage"].CGImage);
    [cell.layer renderInContext:context];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

#pragma mark tableView代理
- (void)tableView:(UITableView *)tableView
      willDisplayCell:(UITableViewCell *)cell
    forRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *sStr            = [NSString stringWithFormat:@"%ld", indexPath.section];
    NSString *rStr            = [NSString stringWithFormat:@"%ld", indexPath.row];
    self.indexPathDic[@"top"] = @{sStr : rStr};
}
- (void)tableView:(UITableView *)tableView
    didEndDisplayingCell:(UITableViewCell *)cell
       forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *sStr             = [NSString stringWithFormat:@"%ld", indexPath.section];
    NSString *rStr             = [NSString stringWithFormat:@"%ld", indexPath.row];
    self.indexPathDic[@"down"] = @{sStr : rStr};
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
